programming4us
           
 
 
Programming

iPad SDK : Preparing Dudel for a New Tool (part 4) - Creating a New Drawable Class

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
3/8/2011 3:27:06 PM

5. Creating a New Drawable Class

Once again, create a new class in Xcode, and name it TextDrawingInfo. The code for this is pretty similar to the PathDrawingInfo class from this article, but here we're keeping track of a slightly different set of details and providing a different set of methods for creating new instances. TextDrawingInfo.h looks like this:

//  TextDrawingInfo.h
#import <Foundation/Foundation.h>
#import "Drawable.h"
@interface TextDrawingInfo : NSObject <Drawable> {
UIBezierPath *path;
UIColor *strokeColor;
UIFont *font;
NSString *text;
}
@property (retain, nonatomic) UIBezierPath *path;
@property (retain, nonatomic) UIColor *strokeColor;
@property (retain, nonatomic) UIFont *font;
@property (copy, nonatomic) NSString *text;
- (id)initWithPath:(UIBezierPath*)p text:(NSString*)t strokeColor:(UIColor*)s font:(UIFont*)f;
+ (id)textDrawingInfoWithPath:(UIBezierPath *)p text:t strokeColor:(UIColor *)s font:(UIFont *)f;
@end


As for the implementation, TextDrawingInfo.m is pretty straightforward. The only interesting method is the draw method. Here's the whole thing:

//  TextDrawingInfo.m
#import "TextDrawingInfo.h"
#import <CoreText/CoreText.h>

@implementation TextDrawingInfo
@synthesize path, strokeColor, font, text;
- initWithPath:(UIBezierPath*)p text:(NSString*)t strokeColor:(UIColor*)s font:(UIFont*)f {
if ((self = [self init])) {
path = [p retain];
strokeColor = [s retain];
font = [f retain];


text = [t copy];
}
return self;
}
+ (id)textDrawingInfoWithPath:(UIBezierPath *)p text:t strokeColor:(UIColor *)s font:(UIFont *)f {
return [[[self alloc] initWithPath:p text:t strokeColor:s font:f] autorelease];
}
- (void)dealloc {
self.path = nil;
self.strokeColor = nil;
self.font = nil;
self.text = nil;
[super dealloc];
}
- (void)draw {
CGContextRef context = UIGraphicsGetCurrentContext();

NSMutableAttributedString *attrString =
[[[NSMutableAttributedString alloc] initWithString:self.text] autorelease];
[attrString addAttribute:(NSString *)(kCTForegroundColorAttributeName)
value:(id)self.strokeColor.CGColor
range:NSMakeRange(0, [self.text length])];
CTFramesetterRef framesetter =
CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrString);

CTFrameRef frame = CTFramesetterCreateFrame(framesetter,
CFRangeMake(0, [attrString length]),
self.path.CGPath, NULL);
CFRelease(framesetter);
if (frame) {
CGContextSaveGState(context);

// Core Text wants to draw our text upside down! This flips it the
// right way.
CGContextTranslateCTM(context, 0, path.bounds.origin.y);
CGContextScaleCTM(context, 1, −1);
CGContextTranslateCTM(context, 0, -(path.bounds.origin.y + path.bounds.size.height));

CTFrameDraw(frame, context);
CGContextRestoreGState(context);
CFRelease(frame);
}
}
@end


You may recognize some pieces of the draw method. We create an attributed string and set its color, use it to create a CTFramesetterRef, and then use that to create a CTFrameRef. Here, we've added a check to make sure the CTFrameRef is created and also a few CGContext function calls. These are here for a specific reason: On the Mac OS X platform, the y axis is flipped (relative to the way it's done on iOS) for normal drawing. Core Text, which comes from Mac OS X, expects that flipped axis, which means that when it draws in an iOS context, the results are upside down! The translate/scale/translate triad in the preceding code makes sure that the text appears right side up.

Before this will compile, we need to add the Core Text framework to the Xcode project. Right-click the Frameworks folder, select Add => Existing Frameworks... from the context menu, select CoreText.framework from the list that appears, and then click the Add button.

Now that everything is in place, you should be able to build and run Dudel in the simulator, and use the new Text tool to add text to your drawings. The masterpiece on display in Figure 5-5 just scratches the surface of what can be done here.

Figure 5. This is the only joke I can consistently remember, people—seriously.
Other -----------------
- jQuery 1.3 : AJAX - Loading data on demand (part 3) - Loading an XML document
- jQuery 1.3 : AJAX - Loading data on demand (part 2) - Working with JavaScript objects
- jQuery 1.3 : AJAX - Loading data on demand (part 1) - Appending HTML
- Coding JavaScript for Mobile Browsers (part 13) - Zoom and rotate gestures
- Coding JavaScript for Mobile Browsers (part 12) - Swipe gesture
- Coding JavaScript for Mobile Browsers (part 11)
- Coding JavaScript for Mobile Browsers (part 10) - Event Handling
- Coding JavaScript for Mobile Browsers (part 9) - Scripting Styles
- Coding JavaScript for Mobile Browsers (part 8) - DOM
- Coding JavaScript for Mobile Browsers (part 7)
- Coding JavaScript for Mobile Browsers (part 6)
- iPad SDK : The Structure of Core Text
- iPad SDK : PDF Generation
- jQuery 1.3 : Sorting and paging (part 5) - Finessing the sort keys
- jQuery 1.3 : Sorting and paging (part 4)
- jQuery 1.3 : Sorting and paging (part 3) - Using a comparator to sort table rows
- jQuery 1.3 : Sorting and paging (part 2) - JavaScript sorting
- jQuery 1.3 : Sorting and paging (part 1) - Server-side sorting
- Coding JavaScript for Mobile Browsers (part 5)
- Coding JavaScript for Mobile Browsers (part 4)
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us